//-----------------------------------------------------------------------
// <copyright file="TerritoryDAOBase.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Collections;
using MyFramework;
using MyFramework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MIMS.WRS.Entities;

namespace MIMS.WRS.Data
{    
    public abstract class TerritoryDAOBase
    {
        #region Common methods
        public virtual Territory CreateTerritoryFromReader(IDataReader reader)
        {
            Territory item = new Territory();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("TerritoryID"))) item.TerritoryID = (int)reader["TerritoryID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Description"))) item.Description = (string)reader["Description"];
                if (!reader.IsDBNull(reader.GetOrdinal("CountryID"))) item.CountryID = (int)reader["CountryID"];
				if (!reader.IsDBNull(reader.GetOrdinal("IDNumber"))) item.IDNumber = (int)reader["IDNumber"];
				if (!reader.IsDBNull(reader.GetOrdinal("ShowHDPricingOnIDS"))) item.ShowHDPricingOnIDS = (bool)reader["ShowHDPricingOnIDS"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateTerritoryFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateTerritory methods
            
        public virtual void CreateTerritory(Territory territory)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spTerritoriesInsert");
                
                dbCommandWrapper.AddInParameter("@Description", DbType.String, territory.Description);
                dbCommandWrapper.AddInParameter("@CountryID", DbType.Int32, territory.CountryID);
				dbCommandWrapper.AddInParameter("@IDNumber", DbType.Int32, territory.IDNumber == Nulls.NullInteger?DBNull.Value:(object)territory.IDNumber);
				dbCommandWrapper.AddInParameter("@ShowHDPricingOnIDS", DbType.Boolean, territory.ShowHDPricingOnIDS);
                dbCommandWrapper.AddOutParameter("@TerritoryID", DbType.Int32, 0);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
                territory.TerritoryID = (int)dbCommandWrapper.GetParameterValue("@TerritoryID");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateTerritoryException, ex);
            }
        }

        #endregion

        #region UpdateTerritory methods
        
        public virtual void UpdateTerritory(Territory territory)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spTerritoriesUpdate");            
                
                dbCommandWrapper.AddInParameter("@TerritoryID", DbType.Int32, territory.TerritoryID);
                dbCommandWrapper.AddInParameter("@Description", DbType.String, territory.Description);
                dbCommandWrapper.AddInParameter("@CountryID", DbType.Int32, territory.CountryID);
				dbCommandWrapper.AddInParameter("@IDNumber", DbType.Int32, territory.IDNumber == Nulls.NullInteger?DBNull.Value:(object)territory.IDNumber);
				dbCommandWrapper.AddInParameter("@ShowHDPricingOnIDS", DbType.Boolean, territory.ShowHDPricingOnIDS);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateTerritoryException, ex);
            }
        }
        
        #endregion

        #region DeleteTerritory methods
        public virtual void DeleteTerritory(int territoryID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spTerritoriesDelete");
                
                dbCommandWrapper.AddInParameter("@TerritoryID", DbType.Int32, territoryID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteTerritoryException, ex);
            }
        }

        #endregion

        #region GetTerritory methods
        
        public virtual Territory GetTerritory(int territoryID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spTerritoriesGet");
                
                dbCommandWrapper.AddInParameter("@TerritoryID", DbType.Int32, territoryID);
                
                Territory territory =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
                {
                    if (reader.Read())
                    {
                        territory = CreateTerritoryFromReader(reader);
                        reader.Close();
                    }
                }
                return territory;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetTerritoryException, ex);
            }
        }
        
        #endregion

        #region GetTerritories methods
        public virtual TerritoryCollection GetTerritories(TerritoryColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spTerritoriesGetList");
                
                dbCommandWrapper.AddInParameter("@OrderBy", DbType.AnsiString, orderBy.ToString());
                dbCommandWrapper.AddInParameter("@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                dbCommandWrapper.AddInParameter("@Page", DbType.Int32, page);
                dbCommandWrapper.AddInParameter("@PageSize", DbType.Int32, pageSize);
                dbCommandWrapper.AddOutParameter("@TotalRecords", DbType.Int32, 4);
                
                TerritoryCollection territoryCollection = new TerritoryCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
                {
                    while (reader.Read())
                    {
                        Territory territory = CreateTerritoryFromReader(reader);
                        territoryCollection.Add(territory);
                    }
                    reader.Close();
                }
                totalRecords = (int)dbCommandWrapper.GetParameterValue("@TotalRecords");
                return territoryCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetTerritoriesException, ex);
            }
        }
        
        public virtual TerritoryCollection GetTerritories(TerritoryColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetTerritories(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}